package com.ruoyi.web.controller.system;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.dto.MoneyRecordImportDto;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.MoneyRecord;
import com.ruoyi.system.service.IMoneyRecordService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 钞币记录Controller
 * 
 * @author ruoyi
 * @date 2025-02-21
 */
@RestController
@RequestMapping("/system/record")
public class MoneyRecordController extends BaseController
{
    @Autowired
    private IMoneyRecordService moneyRecordService;

    /**
     * 查询钞币记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:record:list')")
    @GetMapping("/list")
    public TableDataInfo list(MoneyRecord moneyRecord)
    {
        startPage();
        List<MoneyRecord> list = moneyRecordService.selectByMy(moneyRecord);
        return getDataTable(list);
    }

    /**
     * 查询钞币记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:record:lisAll')")
    @GetMapping("/listAll")
    public TableDataInfo listAll(MoneyRecord moneyRecord)
    {
        startPage();
        List<MoneyRecord> list = moneyRecordService.selectList(moneyRecord);
        return getDataTable(list);
    }

    /**
     * 导出钞币记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:record:export')")
    @Log(title = "钞币记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MoneyRecord moneyRecord)
    {
        List<MoneyRecord> list = moneyRecordService.selectByMy(moneyRecord);
        ExcelUtil<MoneyRecord> util = new ExcelUtil<MoneyRecord>(MoneyRecord.class);
        util.exportExcel(response, list, "钞币记录数据");
    }

    /**
     * 获取钞币记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:record:query')")
    @GetMapping(value = "/{moneyRecordId}")
    public AjaxResult getInfo(@PathVariable("moneyRecordId") Long moneyRecordId)
    {
        return success(moneyRecordService.selectMoneyRecordByMoneyRecordId(moneyRecordId));
    }

    /**
     * 新增钞币记录
     */
    @PreAuthorize("@ss.hasPermi('system:record:add')")
    @Log(title = "钞币记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MoneyRecord moneyRecord)
    {
        return toAjax(moneyRecordService.insertMoneyRecord(moneyRecord));
    }
    /**
     * 新增钞币记录
     */
    @PreAuthorize("@ss.hasPermi('system:record:add')")
    @Log(title = "钞币记录", businessType = BusinessType.INSERT)
    @PostMapping("/addList")
    public AjaxResult addList(@RequestBody List<MoneyRecord> moneyRecords)
    {
        return toAjax(moneyRecordService.insertList(moneyRecords));
    }


    /**
     * 修改钞币记录
     */
    @PreAuthorize("@ss.hasPermi('system:record:edit')")
    @Log(title = "钞币记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MoneyRecord moneyRecord)
    {
        return toAjax(moneyRecordService.updateMoneyRecord(moneyRecord));
    }

    /**
     * 删除钞币记录
     */
    @PreAuthorize("@ss.hasPermi('system:record:remove')")
    @Log(title = "钞币记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{moneyRecordIds}")
    public AjaxResult remove(@PathVariable Long[] moneyRecordIds)
    {
        return toAjax(moneyRecordService.deleteMoneyRecordByMoneyRecordIds(moneyRecordIds));
    }

    @PostMapping("/import")
    @Operation(summary = "导入记录")
    @Parameters({
            @Parameter(name = "file", description = "Excel 文件", required = true),
    })
    @PreAuthorize("@ss.hasPermi('system:record:add')")
    public TableDataInfo importExcel(@RequestParam("file") MultipartFile file) throws Exception {
        ExcelUtil<MoneyRecordImportDto> util = new ExcelUtil<>(MoneyRecordImportDto.class);
        List<MoneyRecordImportDto> recordImportDtos = util.importExcel(file.getInputStream());
        recordImportDtos = moneyRecordService.filterRecord(recordImportDtos);
        recordImportDtos.forEach(item ->{
            item.setCreatedTime(new Date());
        });
        return getDataTable(recordImportDtos);
    }

}
